/** *************************
 * Project: ePoverty
 * Filename: ObjectTableModel.java
 * Description: Table Model that accounts for data types to allow for proper sorting.
 * Name: Bunna Veth
 * Date: April 10, 2012
 *************************** */
package epoverty;

import javax.swing.table.DefaultTableModel;

public class ObjectTableModel extends DefaultTableModel
{
    private Object[][] tableData;
    private String[] columnNames;
    private String[] columnClasses;
    private int numberOfRows;
    private int numberOfColumns;

    //Constructor
    public ObjectTableModel(Object[][] data, String[] headers, String[] classes)
    {
        super(data, headers);
        tableData = data;
        columnNames = headers;
        columnClasses = classes;
        numberOfRows = tableData.length;
        numberOfColumns = columnNames.length;
    }

    //Retrieve Row Data
    public Object[] getRowData(int i)
    {
        return tableData[i];
    }

    //Retrieve Column Data
    public Object[] getColumnData(int n)
    {
        Object[] data = new Object[numberOfRows];
        for (int i = 0; i < numberOfRows; i++)
            data[i] = tableData[i][n];
        return data;
    }

    //Column Class (allows numbers and dates to be displayed and sorted properly)
    @Override
    public Class getColumnClass(int i)
    {
        //load class from string name
        try
        {
            return Class.forName(columnClasses[i]);
        }
        catch (ClassNotFoundException e)
        {
            System.err.println("Class Not Found. Unable to load " + columnClasses[i]);
        }

        //if class not found, just represent the column as a generic object
        return Object.class;
    }

    //Makes table uneditable
    @Override
    public boolean isCellEditable(int row, int col)
    {
        return false;
    }
}